﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RussiaLibrary
{
    public class Class_ControlArray
    {
        public byte[,] Bytes_TetrisByte;
        public Class_Polygon ControlPolygon;//初始化当前操作的方块
        public Class_Polygon NextControlPolygon;//初始化下一个即将操作的方块；
        private void ResetTetrisByte()
        {
            Bytes_TetrisByte = new byte[Class_Common.Length, Class_Common.Width]{
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//0
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//1
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//2
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//3
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//4
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//5
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//6
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//7
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//8
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//9
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//10
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//11
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//12
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//13
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//14
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//15
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//16
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//17
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//18
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//19
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//20
                    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} //21
            };
        }
        public Class_ControlArray()
        {
            ResetTetrisByte();
        }
        public void InitNextPolygon()
        {
            NextControlPolygon = null;
            int random = Class_Common.GetRandomInt() % 5;
            switch (random)
            {
                case 0://方形方块；
                    NextControlPolygon = new Square();
                    break;
                case 1://T形方块；
                    NextControlPolygon = new TPolygans();
                    break;
                case 2://长条形方块；
                    NextControlPolygon = new IPolygons();
                    break;
                case 3://L形方块；
                    NextControlPolygon = new LPolygon();
                    break;
                case 4://Z形方块
                    NextControlPolygon = new ZPolygans();
                    break;
            }
        }

        public bool  InitPolygon()
        { 
            ControlPolygon = null;
            if (NextControlPolygon == null)
            {
                InitNextPolygon();
            }
            ControlPolygon = NextControlPolygon;
            InitNextPolygon();
            return ControlPolygon.Init(Bytes_TetrisByte);
        }
        public bool MoveDown()
        {
            return ControlPolygon.MoveDown(Bytes_TetrisByte);
        }

        public void MoveRight()
        {
            ControlPolygon.RealMoveRight(Bytes_TetrisByte);
            return;
        }   

        public void MoveLeft()
        {
           ControlPolygon.RealMoveLeft(Bytes_TetrisByte);
            return;
        }

        public void MoveEnd()
        {
            while (ControlPolygon.MoveDown(Bytes_TetrisByte)) { };
        }
    }
}
